package com.eurasia.assignment.infrastructure.hoj;

import com.eurasia.assignment.domain.hoj.HOJSingleCaseJudgement;
import com.eurasia.assignment.service.port.out.HOJSingleCaseJudgementPersistencePort;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
@RequiredArgsConstructor
public class HOJSingleCaseJudgementPersistenceAdapter implements HOJSingleCaseJudgementPersistencePort {
    private final HOJSingleCaseJudgementEntityRepository hojSubmissionRepository;
    private final HOJSingleCaseJudgementEntityMapper mapper = HOJSingleCaseJudgementEntityMapper.INSTANCE;
    @Override
    public HOJSingleCaseJudgement save(HOJSingleCaseJudgement submission) {
        HOJSingleCaseJudgementEntity entity = mapper.fromDomain(submission);
        HOJSingleCaseJudgementEntity saved = hojSubmissionRepository.save(entity);
        return mapper.toDomain(saved);
    }

    @Override
    public Optional<HOJSingleCaseJudgement> findByTestJudgeKey(String testJudgeKey) {
        return hojSubmissionRepository.findByTestJudgeKey(testJudgeKey).map(mapper::toDomain);
    }
}
